package org.abratuhi.kvngroupware.network;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class Message {

	/**/
	public Document d;

	/**/
	public Message(){
		Element root = new Element("message");
		d = new Document(root);
	}
	
	public Message(Element message){
		d = new Document(message);
	}
	
	public static Message createMessage(){
		return new Message();
	}
	
	public static Message fromMessage(String s){
		Message m = new Message();
		m.fromString(s);
		return m;
	}
	
	public String toString(){
		XMLOutputter xmlout = new XMLOutputter();
		StringWriter sw = new StringWriter();
		try {
			xmlout.output(d, sw);
			return sw.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void fromString(String s){
		SAXBuilder saxb = new SAXBuilder();
		StringReader sr = new StringReader(s);
		try {
			d = saxb.build(sr);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Element getRoot(){
		return this.d.getRootElement();
	}
	
	public Element getElement(){
		//TODO
		return null;
	}

}
